#! /bin/sh
# PCP QA Test No. 283
# Move pmcd port to iana registered 44321, and the associated multiple
# port changes for ...
#	pmcd via -p
#	pmcd via PMCD_PORT
#	clients via PMCD_PORT
#
# Copyright (c) 2005 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

signal="$sudo $PCP_BINADM_DIR/pmsignal"
status=0	# success is the default!

_cleanup()
{
    [ -z "$pmcd_pid" ] || $signal -s KILL $pmcd_pid
    unset PMCD_PORT
    _service pmcd restart 2>&1 | _filter_pcp_restart
    _wait_for_pmcd
    _service pmlogger restart 2>&1 | _filter_pcp_restart
    _wait_for_pmlogger
    $sudo rm -f $tmp.*
}

trap "_cleanup; exit \$status" 0 1 2 3 15

# Test is failing on test-centos6-container in CI deep inside
# the avahi code (not the PCP code), so we've decided to
# _notrun on anything related to RHEL 6.
#
case `admin/whatami`
in
    *CentOS6.*)
	_notrun "dodge avahi issue on CentOS 6"
	;;
    *RHEL*6.*)
	_notrun "dodge avahi issue on RHEL 6"
	;;
esac

_filter()
{
    sed -n \
	-e '/^sample/p' \
	-e '/^__pmConnectPMCD(/{
s/(.*)/(HOST)/
s/fd=[0-9][0-9]*/fd=<n>/
p
}'
}

if ! _service pmlogger stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmlogger_end || _exit 1
if ! _service pmcd stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmcd_end || _exit 1

# allow time to cleanup and close all sockets
sleep 3

# figure out which user we will run pmcd and its pmdas as
username=`id -u -n`
sudoopts="-u $username"
id $PCP_USER >/dev/null 2>&1 && sudoopts="-u $PCP_USER"

# real QA test starts here
for pmcd_o in 1 2 3
do
    unset PMCD_PORT
    pmcd_opt=''
    case $pmcd_o
    in
	1)	# default
		;;
	2)	# PMCD_PORT is a list in the environment
		PMCD_PORT="9876,44321,4321"
		export PMCD_PORT
		;;
	3)	# -p option to pmcd
		pmcd_opt="-p 4321 -p 44321 -p 9876"
    esac

    echo
    echo "=== PMCD_PORT=$PMCD_PORT pmcd_opt=$pmcd_opt ==="
    echo "=== PMCD_PORT=$PMCD_PORT pmcd_opt=$pmcd_opt ===" >>$seq_full
    cat >$tmp.sh <<END
#!/bin/sh
PATH=$PATH
$PCP_PMCD_PROG -f -l $tmp.log $pmcd_opt &
echo \$!
END

    # Filter the stderr of this using a temp file
    pmcd_pid=`$sudo $sudoopts sh $tmp.sh 2>$tmp.tmp`
    cat $tmp.tmp 1>&2
    sleep 1

    for client_o in 1 2
    do
	unset PMCD_PORT
	case $client_o
	in
	    1)	# default
		;;
	    2)	# PMCD_PORT is a list in the environment
		PMCD_PORT="9876,4321,44321"
		export PMCD_PORT
		;;
	esac

	echo "--- PMCD_PORT=$PMCD_PORT ---"
	echo "--- PMCD_PORT=$PMCD_PORT ---" >>$seq_full
	pmprobe -v -h localhost -D context sample.long.one >$tmp.tmp 2>&1
	cat $tmp.tmp >>$seq_full
	_filter <$tmp.tmp

    done

    $signal -s TERM $pmcd_pid
    pmcd_pid=''
    wait
    sleep 3
    cat $tmp.log >>$seq_full
    # _filter_pmcd_log <$tmp.log

done

# success, all done
exit
